import { ElMessageBox } from 'element-plus'
import { cloneDeep } from 'lodash-es'
import { baseMessage } from '/@/utils/message'

export const deleteItem = async (deleteFn: (id: string) => any, id: string | number | (string | number)[]) => {
  return new Promise((resolve, reject) => {
    let _id = cloneDeep(id)
    if (Array.isArray(_id)) {
      if (_id.length === 0) {
        baseMessage('请选择要删除的项', 'warning')
        return
      }
      _id = _id.join(',')
    }
    ElMessageBox.confirm('此操作将对当前项进行删除操作，不可撤回', '温馨提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(async () => {
        try {
          const { result } = await deleteFn(String(_id))
          if (result > 0) {
            baseMessage('删除成功')
            return resolve(true)
          }
        } catch (error) {
          baseMessage('删除失败', 'error')
          return reject(false)
        }
      })
      .catch(() => {})
  })
}
